package GUI;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;

import cbcr.CBCRCalculations;
import cbcr.OutputCandidate;

public class CBCRInterface {

	Display display = new Display();
	Shell shell = new Shell(display);
	static Properties props;
	CBCRCalculations cbcrCalculations;
	ArrayList<Integer> percents;

	CBCRInterface() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		gridLayout.makeColumnsEqualWidth = false;
		shell.setText("Case Based Candidate Ranking System");
		shell.setLayout(gridLayout);
		shell.setSize(530, 530);
		cbcrCalculations = new CBCRCalculations();
		percents = new ArrayList<Integer>();

		TabFolder folder = new TabFolder(shell, SWT.NONE);
		CandidateRankingDecisionFactorFormCreator(folder);
		shell.open();
		// Set up the event loop.
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				// If no more entries in event queue
				display.sleep();
			}
		}

		display.dispose();
	}

	final private void ResultDisplayCreator(Display disp, String admissionToProgram) {
                Shell shell = new Shell(disp);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 4;
		gridLayout.makeColumnsEqualWidth = false;
		shell.setText("CBCR Results ");
		shell.setLayout(gridLayout);
		shell.setSize(470, 400);
		

		TabFolder folder = new TabFolder(shell, SWT.NONE);

                GridData gdataL = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		gdataL.widthHint = 100;
		gdataL.heightHint = 30;
		Font font = new Font(display, "Calibri", 13, SWT.BOLD | SWT.CENTER);
		Font fontl = new Font(display, "Calibri", 12, SWT.NONE);
		Font fontex = new Font(display, "Calibri", 10, SWT.NONE);

	

		TabItem tab2 = new TabItem(folder, SWT.NONE);
		tab2.setText("Results");
		// Create the SashForm with HORIZONTAL
		SashForm sashFormGen = new SashForm(folder, SWT.HORIZONTAL);
		Group group = new Group(sashFormGen, SWT.NONE);
		group.setLayout(gridLayout);
		Label head1 = new Label(group, SWT.NONE);
		head1.setText("ID");
		// head1.setLayoutData(gdatafl);
		head1.setFont(font);
		Label head2 = new Label(group, SWT.NONE);
		head2.setText("Name");
		// head1.setLayoutData(gdatafl);
		head2.setFont(font);
		Label head3 = new Label(group, SWT.NONE);
		head3.setText("Rankings");
		head3.setFont(font);
		// head1.setLayoutData(gdatafl);
		Label head4 = new Label(group, SWT.NONE);
		head4.setText("");
		head4.setFont(font);
		// head1.setLayoutData(gdatafl);

		ArrayList<OutputCandidate> results = cbcrCalculations
				.calculateRatings(percents, admissionToProgram);

		for (OutputCandidate newCandidate : results) {
			Label idl = new Label(group, SWT.NONE);
			idl.setText("" + newCandidate.getID());
			idl.setFont(fontl);
			Label namel = new Label(group, SWT.NONE);
			namel.setText(newCandidate.getName());
			namel.setFont(fontl);
			ProgressBar pb2 = new ProgressBar(group, SWT.SMOOTH);
			pb2.setSelection((int) newCandidate.getTotalScore() * 10);
			pb2.setBounds(100, 40, 200, 20);
			Label ratingl = new Label(group, SWT.NONE);
			ratingl.setText("" + newCandidate.getTotalScore());
			ratingl.setFont(fontl);
			// explanations
			Label explanations = new Label(group, SWT.NONE);
			explanations.setText("Explanations:");
			explanations.setFont(fontl);
			Label spacefiller1 = new Label(group, SWT.NONE);
			Label spacefiller2 = new Label(group, SWT.NONE);
			Label spacefiller3 = new Label(group, SWT.NONE);
			Label grel = new Label(group, SWT.NONE);
			grel.setText("GRE:");
			grel.setFont(fontex);
			Label grelb = new Label(group, SWT.NONE);
			grelb.setText("" + newCandidate.getGREScore());
			grelb.setFont(fontex);
			Label toefll = new Label(group, SWT.NONE);
			toefll.setText("TOEFL:");
			toefll.setFont(fontex);
			Label toefllb = new Label(group, SWT.NONE);
			toefllb.setText("" + newCandidate.getTOEFLScore());
			toefllb.setFont(fontex);
			Label gpal = new Label(group, SWT.NONE);
			gpal.setText("GPA:");
			gpal.setFont(fontex);
			Label gpalb = new Label(group, SWT.NONE);
			gpalb.setText("" + newCandidate.getGpa());
			gpalb.setFont(fontex);
			Label publ = new Label(group, SWT.NONE);
			publ.setText("Publications:");
			publ.setFont(fontex);
			Label publb = new Label(group, SWT.NONE);
			publb.setText("" + newCandidate.getPublicationScore());
			publb.setFont(fontex);
			Label recol = new Label(group, SWT.NONE);
			recol.setText("Recommendations:");
			recol.setFont(fontex);
			Label recolb = new Label(group, SWT.NONE);
			recolb.setText("" + newCandidate.getRecommendationScore());
			recolb.setFont(fontex);
			ArrayList<Integer> list1 = newCandidate
					.getRecommendationExplanationCaseIds();
			if (list1 != null) {
				String str = "";
				if (list1.size() > 0) {
					Label einstl2 = new Label(group, SWT.NONE);
					einstl2.setText("Relevant Case IDs:");
					einstl2.setFont(fontex);
				} else {
					Label einstl2 = new Label(group, SWT.NONE);
					einstl2.setText("");
				}
				for (int j = 0; j < list1.size(); j++) {
					if (str.equals(""))
						str = str + list1.get(j);
					else
						str = str + ", " + list1.get(j);
				}
				Label einstlb2 = new Label(group, SWT.NONE);
				einstlb2.setText("" + str);
				einstlb2.setFont(fontex);
			} else {
				Label einstl2 = new Label(group, SWT.NONE);
				einstl2.setText("");
				Label einstlb2 = new Label(group, SWT.NONE);
				einstlb2.setText("");
			}
			Label einstl = new Label(group, SWT.NONE);
			einstl.setText("Educational Institute:");
			einstl.setFont(fontex);
			Label einstlb = new Label(group, SWT.NONE);
			einstlb.setText(""
					+ newCandidate.getEducationaInstituteBasedScore());
			einstlb.setFont(fontex);

			ArrayList<Integer> list = newCandidate
					.getEducationalInstituteExplanationCaseIds();
			if (list != null) {
				String str = "";
				if (list.size() > 0) {
					Label einstl2 = new Label(group, SWT.NONE);
					einstl2.setText("Relevant Case IDs:");
					einstl2.setFont(fontl);
				} else {
					Label einstl2 = new Label(group, SWT.NONE);
					einstl2.setText("");
				}
				for (int j = 0; j < list.size(); j++) {
					if (str.equals(""))
						str = str + list.get(j);
					else
						str = str + ", " + list.get(j);
				}
				Label einstlb2 = new Label(group, SWT.NONE);
				einstlb2.setText("" + str);
				einstlb2.setFont(fontl);
			} else {
				Label einstl2 = new Label(group, SWT.NONE);
				einstl2.setText("");
				Label einstlb2 = new Label(group, SWT.NONE);
				einstlb2.setText("");
			}
			
			Label emptyLine1 = new Label(group, SWT.NONE);
			Label emptyLine2 = new Label(group, SWT.NONE);
			Label emptyLine3 = new Label(group, SWT.NONE);
			Label emptyLine4 = new Label(group, SWT.NONE);
		}

		tab2.setControl(sashFormGen);


		shell.open();
		// Set up the event loop.
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				// If no more entries in event queue
				display.sleep();
			}
		}

	}

	private void CandidateRankingDecisionFactorFormCreator(TabFolder ifolder) {
		final TabFolder folder = ifolder;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		gridLayout.makeColumnsEqualWidth = false;
		GridData gdataL = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		gdataL.widthHint = 150;
		gdataL.heightHint = 35;
		GridData gdataT = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		gdataT.widthHint = 100;
		gdataT.heightHint = 30;
		GridData gdatafl = new GridData(GridData.HORIZONTAL_ALIGN_END);
		gdatafl.widthHint = 100;
		gdatafl.heightHint = 30;
		Font font = new Font(display, "Calibri", 12, SWT.BOLD);
		Font fontl = new Font(display, "Calibri", 11, SWT.NONE);

		TabItem tab1 = new TabItem(folder, SWT.NONE);
		tab1.setText("Candidate Ranking Decision Factor Form");
		// Create the SashForm with HORIZONTAL
		SashForm sashFormGen = new SashForm(folder, SWT.HORIZONTAL);
		Group group = new Group(sashFormGen, SWT.NONE);
		group.setLayout(gridLayout);

		// row 1
		Label forl = new Label(group, SWT.NONE);
		forl.setText("For: ");
		forl.setLayoutData(gdatafl);
		forl.setFont(font);
		Label schooll = new Label(group, SWT.NONE);
		schooll.setText("School: ");

		schooll.setFont(fontl);
		final Combo schoolDropDown = new Combo(group, SWT.DROP_DOWN
				| SWT.BORDER);
		ArrayList<String> schools = cbcrCalculations.getFromUnivSpecificInfo(
				"School", "", "", "");
		if (schools != null) {
			for (int i = 0; i < schools.size(); i++) {
				schoolDropDown.add(schools.get(i));
			}
		}
		schooll.setLayoutData(gdataL);

		// row 2
		Label spacefiller = new Label(group, SWT.NONE);
		Label programl = new Label(group, SWT.NONE);
		programl.setText("Program: ");
		programl.setLayoutData(gdataL);
		programl.setFont(fontl);
		final Combo programDropDown = new Combo(group, SWT.DROP_DOWN
				| SWT.BORDER);
		programDropDown.setLayoutData(gdataT);

		// row 3
		Label spacefiller2 = new Label(group, SWT.NONE);
		Label majorl = new Label(group, SWT.NONE);
		majorl.setText("Major: ");
		majorl.setLayoutData(gdataL);
		majorl.setFont(fontl);
		final Combo majorDropDown = new Combo(group, SWT.DROP_DOWN | SWT.BORDER);
		majorDropDown.setLayoutData(gdataT);

		// row 4
		Label infol = new Label(group, SWT.NONE);
		infol.setText("Parameters: ");
		infol.setLayoutData(gdatafl);
		infol.setFont(font);
		Label grel = new Label(group, SWT.NONE);
		grel.setText("GRE %: ");
		grel.setLayoutData(gdataL);
		grel.setFont(fontl);
		final Text gret = new Text(group, SWT.BORDER);
		gret.setLayoutData(gdataT);

		// row 5
		Label spacefiller5 = new Label(group, SWT.NONE);
		Label toefll = new Label(group, SWT.NONE);
		toefll.setText("TOEFL %: ");
		toefll.setLayoutData(gdataL);
		toefll.setFont(fontl);
		final Text toeflt = new Text(group, SWT.BORDER);
		toeflt.setLayoutData(gdataT);

		// row 6
		Label spacefiller6 = new Label(group, SWT.NONE);
		Label eduinstil = new Label(group, SWT.NONE);
		eduinstil.setText("Educational Institute %: ");
		eduinstil.setLayoutData(gdataL);
		eduinstil.setFont(fontl);
		final Text eduinstit = new Text(group, SWT.BORDER);
		eduinstit.setLayoutData(gdataT);

		// row 7
		Label spacefiller7 = new Label(group, SWT.NONE);
		Label gpal = new Label(group, SWT.NONE);
		gpal.setText("GPA %: ");
		gpal.setLayoutData(gdataL);
		gpal.setFont(fontl);
		final Text gpat = new Text(group, SWT.BORDER);
		gpat.setLayoutData(gdataT);

		// row 8
		Label spacefiller8 = new Label(group, SWT.NONE);
		Label publ = new Label(group, SWT.NONE);
		publ.setText("Publication %: ");
		publ.setLayoutData(gdataL);
		publ.setFont(fontl);
		final Text pubt = new Text(group, SWT.BORDER);
		pubt.setLayoutData(gdataT);

		// row 9
		Label spacefiller9 = new Label(group, SWT.NONE);
		Label recol = new Label(group, SWT.NONE);
		recol.setText("Recommendations %: ");
		recol.setLayoutData(gdataL);
		recol.setFont(fontl);
		final Text recot = new Text(group, SWT.BORDER);
		recot.setLayoutData(gdataT);

		// row 10
		Label spacefiller10 = new Label(group, SWT.NONE);
		Label relexl = new Label(group, SWT.NONE);
		relexl.setText("Relevant Experience %: ");
		relexl.setLayoutData(gdataL);
		relexl.setFont(fontl);
		final Text relext = new Text(group, SWT.BORDER);
		relext.setLayoutData(gdataT);

		// row 11 - submit button
		Label spacefiller11 = new Label(group, SWT.NONE);
		Button submitButton = new Button(group, SWT.PUSH);
		// submitButton.setFont(font);
		GridData gdataS = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		gdataS.widthHint = 100;
		gdataS.heightHint = 30;
		submitButton.setLayoutData(gdataS);
		submitButton.setText("Save");
		submitButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {

				if (gret.getText().isEmpty() || toeflt.getText().isEmpty()
						|| eduinstit.getText().isEmpty()
						|| gpat.getText().isEmpty() || pubt.getText().isEmpty()
						|| recot.getText().isEmpty()
						|| relext.getText().isEmpty()) {
					MessageBox messageBox = new MessageBox(shell, SWT.OK
							| SWT.ICON_WARNING);
					messageBox.setMessage("Please fill in all fields.");
					messageBox.open();
				} else {
					percents.add(Integer.valueOf(gret.getText()).intValue());
					percents.add(Integer.valueOf(toeflt.getText()).intValue());
					percents.add(Integer.valueOf(eduinstit.getText())
							.intValue());
					percents.add(Integer.valueOf(gpat.getText()).intValue());
					percents.add(Integer.valueOf(pubt.getText()).intValue());
					percents.add(Integer.valueOf(recot.getText()).intValue());
					percents.add(Integer.valueOf(relext.getText()).intValue());
				}

				if ((schoolDropDown.getSelectionIndex() > -1)
						&& (programDropDown.getSelectionIndex() > -1)
						&& (majorDropDown.getSelectionIndex() > -1)) {
					String school = schoolDropDown.getItem(schoolDropDown
							.getSelectionIndex());
					String program = programDropDown.getItem(programDropDown
							.getSelectionIndex());
					String major = majorDropDown.getItem(majorDropDown
							.getSelectionIndex());
					cbcrCalculations.saveParameterpercents(percents, school,
							program, major);

					MessageBox messageBox = new MessageBox(shell, SWT.OK
							| SWT.ICON_WARNING);
					messageBox.setMessage("Database is updated.");
					messageBox.open();
				}

			}
		});

		Button calculateButton = new Button(group, SWT.PUSH);
		calculateButton.setLayoutData(gdataS);
		calculateButton.setText("Calculate Ratings");
		calculateButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (gret.getText().isEmpty() || toeflt.getText().isEmpty()
						|| eduinstit.getText().isEmpty()
						|| gpat.getText().isEmpty() || pubt.getText().isEmpty()
						|| recot.getText().isEmpty()
						|| relext.getText().isEmpty()) {
					MessageBox messageBox = new MessageBox(shell, SWT.OK
							| SWT.ICON_WARNING);
					messageBox.setMessage("Please fill in all fields.");
					messageBox.open();
				} else {
					percents.add(Integer.valueOf(gret.getText()).intValue());
					percents.add(Integer.valueOf(toeflt.getText()).intValue());
					percents.add(Integer.valueOf(eduinstit.getText())
							.intValue());
					percents.add(Integer.valueOf(gpat.getText()).intValue());
					percents.add(Integer.valueOf(pubt.getText()).intValue());
					percents.add(Integer.valueOf(recot.getText()).intValue());
					percents.add(Integer.valueOf(relext.getText()).intValue());
				}

				if ((schoolDropDown.getSelectionIndex() > -1)
						&& (programDropDown.getSelectionIndex() > -1)
						&& (majorDropDown.getSelectionIndex() > -1)) {
					String school = schoolDropDown.getItem(schoolDropDown
							.getSelectionIndex());
					String program = programDropDown.getItem(programDropDown
							.getSelectionIndex());
					String major = majorDropDown.getItem(majorDropDown
							.getSelectionIndex());

					ResultDisplayCreator(display, program);

					/*MessageBox messageBox = new MessageBox(shell, SWT.OK
							| SWT.ICON_WARNING);
					messageBox.setMessage("Ratings are calculated.");
					messageBox.open();*/
				}

			}
		});

		// event handlers
		schoolDropDown.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if (schoolDropDown.getSelectionIndex() > -1) {
					String school = schoolDropDown.getItem(schoolDropDown
							.getSelectionIndex());
					ArrayList<String> programs = cbcrCalculations
							.getFromUnivSpecificInfo("Program", school, "", "");
					if (programs != null) {
						for (int i = 0; i < programs.size(); i++) {
							programDropDown.add(programs.get(i));
						}
					}
				}
			}
		});

		programDropDown.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if ((schoolDropDown.getSelectionIndex() > -1)
						&& (programDropDown.getSelectionIndex() > -1)) {
					String school = schoolDropDown.getItem(schoolDropDown
							.getSelectionIndex());
					String program = programDropDown.getItem(programDropDown
							.getSelectionIndex());
					ArrayList<String> majors = cbcrCalculations
							.getFromUnivSpecificInfo("Major", school, program,
									"");
					if (majors != null) {
						for (int i = 0; i < majors.size(); i++) {
							majorDropDown.add(majors.get(i));
						}
					}
				}
			}
		});

		majorDropDown.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if ((schoolDropDown.getSelectionIndex() > -1)
						&& (programDropDown.getSelectionIndex() > -1)
						&& (majorDropDown.getSelectionIndex() > -1)) {
					String school = schoolDropDown.getItem(schoolDropDown
							.getSelectionIndex());
					String program = programDropDown.getItem(programDropDown
							.getSelectionIndex());
					String major = majorDropDown.getItem(majorDropDown
							.getSelectionIndex());
					ArrayList<String> paras = cbcrCalculations
							.getFromUnivSpecificInfo("AllParameters", school,
									program, major);
					if (paras != null) {
						gret.setText(paras.get(0));
						toeflt.setText(paras.get(1));
						eduinstit.setText(paras.get(2));
						gpat.setText(paras.get(3));
						pubt.setText(paras.get(4));
						recot.setText(paras.get(5));
						relext.setText(paras.get(6));
					}
				}
			}
		});

		tab1.setControl(sashFormGen);

	}

	public static void main(String args[]) {
		new CBCRInterface();
	}
}
